package kr.hyosang.android.penfo.processor;

import java.net.MalformedURLException;

import kr.hyosang.HttpUtil;
import kr.hyosang.android.penfo.Definition;
import kr.hyosang.android.penfo.SharedObject;
import kr.hyosang.android.penfo.parser.Parser;
import android.util.Log;

public class SendComment
{
    public SendComment() {
    }
    
    public boolean submit(String boardId, String boardNo, String comment) {
        HttpUtil httpUtil = new HttpUtil();
        
        try {
            httpUtil.setURL(Definition.URL.SUBMIT_COMMENT);
            httpUtil.addPostValue("id", boardId);
            httpUtil.addPostValue("no", boardNo);
            httpUtil.addPostValue("memo", comment);
            
            httpUtil.setRequestProperty("Referer", Definition.URL.BOARD);
            httpUtil.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            SharedObject shared = SharedObject.getInstance();
            
            String cookie = String.format("PHPSESSID=%s; ZBSESSIONID=%s", shared.getPHPSESSID(), shared.getZBSESSIONID());
            Log.d("penfo", "Cookie : " + cookie);
            
            if(cookie != null && !"".equals(cookie)) {
                httpUtil.setRequestProperty("Cookie", cookie);
            }
            
            httpUtil.process();
            
            //Set-Cookie 처리
            SharedObject.getInstance().setCookies(httpUtil.getHeaderFields("Set-Cookie"));
            
            String html = httpUtil.getHtml();
            
            if(Parser.isRedirectPage(html)) {
                return true;
            }else {
                return false;
            }
        }catch(MalformedURLException e) {
            e.printStackTrace();
        }
        
        return true;
    }

}
